package com.jet.sms.service;

import com.jet.core.enums.IEnum;
import com.jet.core.exception.CustomException;
import com.jet.sms.domain.dto.*;
import com.jet.sms.enums.SmsTempTypeEnum;
import com.jet.core.utils.AmountUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信异步通知
 * <p>
 * 商户通知类短信都走这儿发
 * <p>
 * 1.会员注册
 * <p>
 * 2.会员储值
 * <p>
 * 3.储值变动
 * <p>
 * 4.会员计次卡
 * <p>
 * 5.计次卡余额变动
 * <p>
 * 6.销售通知
 * <p>
 * 7.退货通知
 * <p>
 * 8.会员预约
 * <p>
 * 9.生日祝福
 *
 * @author dcl
 * @since 2024/6/18 16:48
 */
@Service
@Slf4j
public class SmsSendNoticeService {

    @Resource
    private SmsTemplateSettingService smsTemplateSettingService;

    @Resource
    private SmsSendService smsSendService;

    public void memberRegisterSend(SmsMemberRegisterDto dto) {
        validSmsSend(dto.getMerchantNo(), SmsTempTypeEnum.MEMBER_REGISTER);
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("memberName", dto.getMemberName());
        tempParams.put("memberSex", dto.getSex());
        tempParams.put("shopName", dto.getStoreName());

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(dto.getMemberId(), dto.getPhone()));
        SmsSendNoticeDto noticeDto = new SmsSendNoticeDto();
        noticeDto.setMerchantNo(dto.getMerchantNo());
        noticeDto.setStoreNo(dto.getStoreNo());
        noticeDto.setAccountId(dto.getAccountId());
        noticeDto.setTempType(SmsTempTypeEnum.MEMBER_REGISTER.getCode());
        noticeDto.setUsers(users);
        noticeDto.setTempParams(tempParams);
        smsSend(noticeDto);
    }

    /**
     * 会员储值短信通知
     *
     * @param noticeDto
     */
    public void memberRechargeSend(MemberRechargeNoticeDto noticeDto) {
        validSmsSend(noticeDto.getMerchantNo(), SmsTempTypeEnum.MEMBER_RECHARGE);
        // 检查储值变更是否发送短信，积分变更是否发送短信，并发送
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("shopName", noticeDto.getStoreName());
        tempParams.put("realAmount", AmountUtil.parseYuan(noticeDto.getChangeRecharge()));
        tempParams.put("surplusAmount", AmountUtil.parseYuan(noticeDto.getAfterRecharge()));

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(noticeDto.getMemberId(), noticeDto.getPhone()));
        SmsSendNoticeDto dto = new SmsSendNoticeDto();
        dto.setMerchantNo(noticeDto.getMerchantNo());
        dto.setStoreNo(noticeDto.getStoreNo());
        dto.setAccountId(noticeDto.getAccountId());
        dto.setTempType(SmsTempTypeEnum.MEMBER_RECHARGE.getCode());
        dto.setUsers(users);
        dto.setTempParams(tempParams);
        smsSend(dto);
    }

    /**
     * 会员储值金额变更短信通知
     *
     * @param noticeDto
     */
    public void memberRechargeChangeSend(MemberRechargeNoticeDto noticeDto) {
        validSmsSend(noticeDto.getMerchantNo(), SmsTempTypeEnum.MEMBER_RECHARGE_CHANGE);
        // 检查储值变更是否发送短信，积分变更是否发送短信，并发送
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("shopName", noticeDto.getStoreName());
        tempParams.put("realAmount", AmountUtil.parseYuan(noticeDto.getChangeRecharge()));
        tempParams.put("surplusAmount", AmountUtil.parseYuan(noticeDto.getAfterRecharge()));

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(noticeDto.getMemberId(), noticeDto.getPhone()));
        SmsSendNoticeDto dto = new SmsSendNoticeDto();
        dto.setMerchantNo(noticeDto.getMerchantNo());
        dto.setStoreNo(noticeDto.getStoreNo());
        dto.setAccountId(noticeDto.getAccountId());
        dto.setTempType(SmsTempTypeEnum.MEMBER_RECHARGE_CHANGE.getCode());
        dto.setUsers(users);
        dto.setTempParams(tempParams);
        smsSend(dto);
    }

    public void memberCountedCreateSend(MemberCountedNoticeDto noticeDto) {
        validSmsSend(noticeDto.getMerchantNo(), SmsTempTypeEnum.MEMBER_COUNTED);
        //发送订单成功短信
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("memberName", noticeDto.getMemberName());
        tempParams.put("memberSex", noticeDto.getSex());
        tempParams.put("shopName", noticeDto.getStoreName());
        tempParams.put("timeCardName", noticeDto.getTimeCardName());
        tempParams.put("purchaseTimeCard", String.valueOf(noticeDto.getAddTimeCard()));
        tempParams.put("memberTimeTotal", String.valueOf(noticeDto.getNowTimeCard()));

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(noticeDto.getMemberId(), noticeDto.getPhone()));
        SmsSendNoticeDto dto = new SmsSendNoticeDto();
        dto.setMerchantNo(noticeDto.getMerchantNo());
        dto.setStoreNo(noticeDto.getStoreNo());
        dto.setAccountId(null);
        dto.setTempType(SmsTempTypeEnum.MEMBER_COUNTED.getCode());
        dto.setUsers(users);
        dto.setTempParams(tempParams);
        smsSend(dto);
    }

    /**
     * 计次卡变更短信通知
     *
     * @param dto
     */
    public void timeCardConsumeSms(SmsCountedConsumeDto dto) {
        validSmsSend(dto.getMerchantNo(), SmsTempTypeEnum.MEMBER_COUNTED_CHANGE);
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("memberName", dto.getMemberName());
        tempParams.put("memberSex", dto.getSex());
        tempParams.put("shopName", dto.getStoreName());
        tempParams.put("surplusTotal", String.valueOf(dto.getNowTimeCard()));

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(dto.getMemberId(), dto.getPhone()));
        SmsSendNoticeDto noticeDto = new SmsSendNoticeDto();
        noticeDto.setMerchantNo(dto.getMerchantNo());
        noticeDto.setStoreNo(dto.getStoreNo());
        noticeDto.setAccountId(null);
        noticeDto.setTempType(SmsTempTypeEnum.MEMBER_COUNTED_CHANGE.getCode());
        noticeDto.setUsers(users);
        noticeDto.setTempParams(tempParams);
        smsSend(noticeDto);
    }

    /**
     * 销售订单短信通知
     *
     * @param dto
     */
    public void cashierOrderSend(SmsCashierOrderDto dto) {
        validSmsSend(dto.getMerchantNo(), SmsTempTypeEnum.ORDER_CASHIER);
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("memberName", dto.getMemberName());
        tempParams.put("memberSex", dto.getSex());
        tempParams.put("memberRealAmount", dto.getPayableAmount().toPlainString());
        tempParams.put("shopName", dto.getStoreName());

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(dto.getMemberId(), dto.getPhone()));
        SmsSendNoticeDto noticeDto = new SmsSendNoticeDto();
        noticeDto.setMerchantNo(dto.getMerchantNo());
        noticeDto.setStoreNo(dto.getStoreNo());
        noticeDto.setAccountId(dto.getAccountId());
        noticeDto.setTempType(SmsTempTypeEnum.ORDER_CASHIER.getCode());
        noticeDto.setUsers(users);
        noticeDto.setTempParams(tempParams);
        smsSend(noticeDto);
    }

    /**
     * 销售退款通知
     *
     * @param dto
     */
    public void cashierRefundSend(SmsCashierRefundDto dto) {
        validSmsSend(dto.getMerchantNo(), SmsTempTypeEnum.ORDER_REFUND);
        //发送订单成功短信
        Map<String, String> tempParams = new HashMap<>();
        tempParams.put("memberName", dto.getMemberName());
        tempParams.put("memberSex", dto.getSex());
        tempParams.put("memberRealAmount", dto.getPayableAmount().toPlainString());
        tempParams.put("shopName", dto.getStoreName());

        List<SmsSendUserDto> users = Collections.singletonList(new SmsSendUserDto(dto.getMemberId(), dto.getPhone()));
        SmsSendNoticeDto noticeDto = new SmsSendNoticeDto();
        noticeDto.setMerchantNo(dto.getMerchantNo());
        noticeDto.setStoreNo(dto.getStoreNo());
        noticeDto.setAccountId(dto.getAccountId());
        noticeDto.setTempType(SmsTempTypeEnum.ORDER_REFUND.getCode());
        noticeDto.setUsers(users);
        noticeDto.setTempParams(tempParams);
        smsSend(noticeDto);
    }

    /**
     * 验证是否设置了发送
     *
     * @param merchantNo
     * @param tempType
     * @return
     */
    private void validSmsSend(long merchantNo, SmsTempTypeEnum tempType) {
        if (!smsTemplateSettingService.isSmsSend(merchantNo, tempType.getCode())) {
            log.info("{}未开启，{}店铺短信发送失败", tempType.getName(), merchantNo);
            return;
        }
    }

    private void smsSend(SmsSendNoticeDto dto) {
        try {
            log.info("{}短信通知发送", IEnum.getNameByCode(dto.getTempType(), SmsTempTypeEnum.class));
            smsSendService.asyncSend(dto);
        } catch (CustomException e) {
            log.error("短信发送失败：{}", e.getResultJson());
        }
    }
}
